var printOutOrderMixin = function (orderName) {
  return {
    mixins:[printCommonOrderMixin('searchOutOrderList')],

    props: {
      orders: {
        type: Array,
        default: function () {
          return [];
        }
      }
    },

    data: function () {
      return {
        templateOrderName: orderName,
        outOrderList: [],
        searchOutOrderList: []
      };
    },

    created: function () {
      var self = this;
      this.setTemplConfig().then(function (config) {
        self.setPrintId();
        self.initTemplConfig(config);
        self.initData();
        self.searchData();
      }).catch(function () {});
    },

    methods: {
      initData: function () {
        var orders = this.orders;
        orders = _.cloneDeep(orders);
        for (var i = 0; i < orders.length; i++) {
          var order = orders[i];
          var createTime = order.createTime;
          var flowCycle = order.flowCycle;
          order.finishTime = moment(createTime).add(flowCycle, 'day').startOf('day').valueOf();
          order.createTime = moment(order.createTime).startOf('day').valueOf();

          var returenTime = order.returenTime;
          if (returenTime !== '') {
            order.returenTime = moment(returenTime).startOf('day').valueOf();
          }

          order._id = SysUtil.getId();
        }
        this.outOrderList = orders;
      },

      initTemplConfig: function (config) {
        var clauseContent = config.clauseContent;
        var text = $(clauseContent).text();
        if (text.trim() === '') {
          clauseContent = '';
        }
        this.clauseContent = clauseContent;

        var columns = config.outOrderColumns;
        CommonTemplateUtil.setColumnConfig(columns, "outOrderColumns");
        this.tableColumns = columns;
      },

      searchData: function () {
        var outOrderList = this.outOrderList;
        outOrderList = _.clone(outOrderList);
        this.searchOutOrderList = outOrderList;
        this.sortData();
      },

      sortData: function () {
        var searchOutOrderList = this.searchOutOrderList;
        var tableColumns = this.tableColumns;
        var sortColumn = _.find(tableColumns, function (tableColumn) {
          return tableColumn.sortType !== undefined && tableColumn.sortType !== '';
        });
        var printOrders = _.clone(searchOutOrderList);
        if (sortColumn !== undefined) {
          var sortType = sortColumn.sortType;
          sortType = sortType === 'ascending' ? 'asc' : 'desc';
          var sortConfig = sortColumn.config;
          var sortField = sortConfig.sortField;
          if (sortField === undefined) {
            sortField = sortConfig.field;
          }
          printOrders = SysUtil.sortDataByField(printOrders, sortField, sortType);
        }
        this.printOrders = printOrders;
      },

      toOrderData: function (tableColumn, type) {
        var sortType = tableColumn.sortType;
        if (sortType !== '') {
          if (sortType === type) {
            tableColumn.sortType = '';
          } else {
            tableColumn.sortType = type;
          }
        } else {
          tableColumn.sortType = type;
          var tableColumns = this.tableColumns;
          for (var i = 0; i < tableColumns.length; i++) {
            var column = tableColumns[i];
            if (!column.hasSort || column === tableColumn) {
              continue;
            }

            if (column.sortType !== '') {
              column.sortType = '';
              break;
            }
          }
        }
        this.sortData();
      }
    },

    components: {
      ShowTextarea: ShowTextarea
    }
  }
};